using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using Softelvdm.SftTabsNET;

namespace Scribble {
    public partial class MainWindow : Form {

        public static MainWindow parentWindow;
        public static int documentCount; // static var which keeps track of the document count

        public MainWindow() {
        	parentWindow = this;

            InitializeComponent();

            documentCount = 0;
            CreateDocument("");
        }

        private void menuFileNew_Click(object sender, EventArgs e) {
            New();
        }

        private void menuFileOpen_Click(object sender, EventArgs e) {
            Open();
        }

        private void menuFileClose_Click(object sender, EventArgs e) {
            CloseView();
        }

        private void menuFileSave_Click(object sender, EventArgs e) {
            Save();
        }

        private void menuFileSaveAs_Click(object sender, EventArgs e) {
            Save();
        }

        private void menuFilePrint_Click(object sender, EventArgs e) {
            Print();
        }

        private void menuFilePrintPreview_Click(object sender, EventArgs e) {
            PrintPreview();
        }

        private void menuFileExit_Click(object sender, EventArgs e) {
            Exit();
        }

        private void menuEditClearAll_Click(object sender, EventArgs e) {
            Clear();
        }

        private void menuPenThickline_Click(object sender, EventArgs e) {
            ThickPen();			
        }

        private void menuPenWidths_Click(object sender, EventArgs e) {
            PenWidthsDlg();
        }

        private void menuViewToolbar_Click(object sender, EventArgs e) {
            toolBar1.Visible = menuViewToolbar.Checked = !toolBar1.Visible;
        }

        private void menuViewStatusbar_Click(object sender, EventArgs e) {
            statusBar1.Visible = menuViewStatusbar.Checked = !statusBar1.Visible;
        }

        private void menuWindowNew_Click(object sender, EventArgs e) {
            New();
        }

        private void menuWindowCascade_Click(object sender, EventArgs e) {
            Cascade();
        }

        private void menuWindowTile_Click(object sender, EventArgs e) {
            Tile();
        }

        private void menuHelpTopics_Click(object sender, EventArgs e) {
            ShowHelpTopics();
        }

        private void menuHelpAbout_Click(object sender, EventArgs e) {
            AboutHelp();
        }

        //About Help
        private void AboutHelp() {
            MessageBox.Show("Scribble Version 1.0+\n\nAdapted to use SftTabs/NET for document selection.", "About Scribble", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //Help Topics
        private void ShowHelpTopics() {
            Help.ShowHelp(this, "..\\..\\help\\scribble.chm");
        }

        //Print
        private void Print() {
            try {
                printDoc.Print();
            } catch (Exception e) {
                MessageBox.Show(e.ToString());
            }
        }
        //PrintPage event handler
        private void ScribblePrintPage(object sender, PrintPageEventArgs ev) {
            try {
                ScribbleView activeView = (ScribbleView)this.ActiveMdiChild;
                ScribbleDoc activeDoc = activeView.GetDocument();

                for (int i = 0; i < activeDoc.strokeList.Count; i++) {
                    Stroke st = (Stroke)activeDoc.strokeList[i];
                    st.DrawStroke(ev.Graphics);
                }
                ev.HasMorePages = false;
            } catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }
        }


        //PrintPreview
        private void PrintPreview() {
            try {
                PrintPreviewDialog prevDlg = new PrintPreviewDialog();
                prevDlg.Document = printDoc;
                prevDlg.Size = new System.Drawing.Size(600, 329);
                prevDlg.ShowDialog();
            } catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }

        }
        //Exit
        private void Exit() {
            Form[] childForm = this.MdiChildren;
            //Make sure to ask for saving the doc before exiting the app
            for (int i = 0; i < childForm.Length; i++)
                childForm[i].Close();

            Application.Exit();

        }
        //Close the View
        private void CloseView() {
            ScribbleView activeView = (ScribbleView)this.ActiveMdiChild;
            activeView.Close();
        }
        //Tile
        private void Tile() {
            this.LayoutMdi(MdiLayout.TileHorizontal);

        }
        //Cascade
        private void Cascade() {
            this.LayoutMdi(MdiLayout.Cascade);

        }
        //Clear the contents of the active document
        private void Clear() {
            ScribbleView activeView = (ScribbleView)this.ActiveMdiChild;
            if (activeView != null) {
                ScribbleDoc activeDoc = activeView.GetDocument();
                activeDoc.DeleteContents();
            }
        }

        //Open an existing document
        private void Open() {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "Scribble Files (*.scb)|*.scb|All Files (*.*)|*.*";
            openDlg.FileName = "";
            openDlg.DefaultExt = ".scb";
            openDlg.CheckFileExists = true;
            openDlg.CheckPathExists = true;

            DialogResult res = openDlg.ShowDialog();

            if (res == DialogResult.OK) {
                if (!(openDlg.FileName).EndsWith(".scb") && !(openDlg.FileName).EndsWith(".SCB"))
                    MessageBox.Show("Unexpected file format", "Scribble", MessageBoxButtons.OK);
                else {
                    if (this.ActiveMdiChild == null)
                        EnableItems();

                    ScribbleDoc newDoc = CreateDocument(openDlg.FileName);
                    newDoc.OpenDocument(openDlg.FileName);
                }
            }

        }

        //Save the document
        private void Save() {
            ScribbleView selectedView = (ScribbleView)this.ActiveMdiChild;
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Filter = "Scribble Files (*.scb)|*.scb|All Files (*.*)|*.*";
            saveDlg.DefaultExt = ".scb";
            if (selectedView.GetDocument().docFileName == "")
                saveDlg.FileName = "Untitled";
            else
                saveDlg.FileName = selectedView.GetDocument().docFileName;
            DialogResult res = saveDlg.ShowDialog();

            if (res == DialogResult.OK)
                selectedView.GetDocument().SaveDocument(saveDlg.FileName);

        }

        //Open new document
        private void New() {
            //If this is the first child window, enable the Menu and Toolbar items
            if (this.ActiveMdiChild == null)
                EnableItems();
            CreateDocument("");
        }

        //NewWindow
        private void NewWindow() {
            ScribbleView activeView = (ScribbleView)this.ActiveMdiChild;
            ScribbleView newView = new ScribbleView(activeView.GetDocument(), parentWindow);
            newView.GetDocument().viewList.Add(newView);
            newView.SetTitle(activeView.GetDocument().docFileName);
            newView.Show();
        }

        //Creates a new document
        private ScribbleDoc CreateDocument(String docName) {
            ScribbleDoc newDoc = new ScribbleDoc(parentWindow, docName);
            documentCount++;
            return newDoc;
        }

        private void PenWidthsDlg() {
            Form f = new Form();

            //Get the document of active view
            ScribbleView activeView = (ScribbleView)this.ActiveMdiChild;
            ScribbleDoc activeDoc = activeView.GetDocument();

            f.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            f.Text = "Pen Widths";

            f.ClientSize = new System.Drawing.Size(352, 125);

            Button button1 = new Button();
            button1.Location = new System.Drawing.Point(264, 20);
            button1.Size = new System.Drawing.Size(75, 23);
            button1.TabIndex = 1;
            button1.Text = "OK";
            button1.DialogResult = System.Windows.Forms.DialogResult.OK;//Make this "OK" button

            Button button2 = new Button();
            button2.Location = new System.Drawing.Point(264, 52);
            button2.Size = new System.Drawing.Size(75, 23);
            button2.TabIndex = 6;
            button2.Text = "Cancel";

            TextBox textBox1 = new TextBox();
            textBox1.Location = new System.Drawing.Point(120, 36);
            textBox1.Text = activeDoc.thinWidth.ToString();
            textBox1.TabIndex = 1;
            textBox1.Size = new System.Drawing.Size(64, 20);

            TextBox textBox2 = new TextBox();
            textBox2.Location = new System.Drawing.Point(120, 76);
            textBox2.Text = activeDoc.thickWidth.ToString();
            textBox2.TabIndex = 2;
            textBox2.Size = new System.Drawing.Size(64, 20);


            Label label1 = new Label();
            label1.Location = new System.Drawing.Point(16, 36);
            label1.Text = "Thin Pen Width:";
            label1.Size = new System.Drawing.Size(88, 16);
            label1.TabIndex = 3;

            Label label2 = new Label();
            label2.Location = new System.Drawing.Point(16, 76);
            label2.Text = "Thick Pen Width:";
            label2.Size = new System.Drawing.Size(88, 16);
            label2.TabIndex = 4;


            f.FormBorderStyle = FormBorderStyle.FixedDialog;
            // Set the MaximizeBox to false to remove the maximize box.
            f.MaximizeBox = false;
            // Set the MinimizeBox to false to remove the minimize box.
            f.MinimizeBox = false;
            // Set the accept button of the form to button1.
            f.AcceptButton = button1;
            // Set the cancel button of the form to button2.
            f.CancelButton = button2;

            f.StartPosition = FormStartPosition.CenterScreen;

            f.Controls.Add(button1);
            f.Controls.Add(button2);
            f.Controls.Add(label1);
            f.Controls.Add(label2);
            f.Controls.Add(textBox1);
            f.Controls.Add(textBox2);

            DialogResult res = f.ShowDialog();

            if (res == System.Windows.Forms.DialogResult.OK) {
                activeDoc.thinWidth = UInt32.Parse(textBox1.Text);
                activeDoc.thickWidth = UInt32.Parse(textBox2.Text);
                activeDoc.ReplacePen();
                f.Close();
            }
        }

        private void ThickPen() {
            ScribbleView activeView = (ScribbleView)this.ActiveMdiChild;
            ScribbleDoc activeDoc = activeView.GetDocument();
            activeDoc.thickPen = !activeDoc.thickPen;
            activeDoc.ReplacePen();
            this.menuPenThick.Checked = activeDoc.thickPen;
        }

        //Disable the menu and toolbar items when there is no active child form
        public void DisableItems() {
            this.menuItemEdit.Visible = false;
            this.menuItemPen.Visible = false;
            this.menuItemWindow.Visible = false;
            this.menuFileClose.Visible = false;
            this.menuFileSave.Visible = false;
            this.menuFileSaveAs.Visible = false;
            this.menuFilePrint.Visible = false;
            this.menuFilePrintPreview.Visible = false;
            this.saveButton.Enabled = false;
            this.previewButton.Enabled = false;
            this.printButton.Enabled = false;

        }

        //Enable the menu and toolbar items when the first child form is created
        public void EnableItems() {
            this.menuItemEdit.Visible = true;
            this.menuItemPen.Visible = true;
            this.menuItemWindow.Visible = true;
            this.menuFileClose.Visible = true;
            this.menuFileSave.Visible = true;
            this.menuFileSaveAs.Visible = true;
            this.menuFilePrint.Visible = true;
            this.menuFilePrintPreview.Visible = true;
            this.saveButton.Enabled = true;
            this.previewButton.Enabled = true;
            this.printButton.Enabled = true;

        }

        private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e) {
            if (e.Button == newButton) {
                New();
            } else if (e.Button == openButton) {
                Open();
            } else if (e.Button == saveButton) {
                Save();
            } else if (e.Button == previewButton) {
                PrintPreview();
            } else if (e.Button == printButton) {
                Print();
            } else if (e.Button == helpButton) {
                ShowHelpTopics();
            }
        }

        //App closing handler
        public void ClosingMainAppHander(Object sender, CancelEventArgs e) {
            this.Exit();
        }

        private void MainWindow_Load(object sender, EventArgs e) {
            // resize the tab control and its container (panel) to use the tab control's optimal height
            // leave a small gap above the tab control
            sftTabs1.MakeNaturalSize();
            panelTabControl.Height = sftTabs1.Height + 4;
            panelTabControl.Top = 4;

            MessageBox.Show("This example is based on the \"Scribble\" sample application included with Visual Studio.NET.  It has been modified to use SftTabs/NET for document selection.", "SftTabs/NET Sample", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void sftTabs1_Switched(object sender, System.EventArgs e) {
            // A new tab has become active, activate the appropriate child window
            if (sftTabs1.Current >= 0) {
                Form f = sftTabs1.TabCollection[sftTabs1.Current].Tag1 as Form;
                if (f != null)
                    f.Activate();
            }
        }

        private void MainWindow_MdiChildActivate(object sender, System.EventArgs e) {
            // An MDI child window is being activated, make the appropriate tab active
            int iTab = -1;
            foreach (TabClass t in sftTabs1.TabCollection) {
                if (t.Tag1 == ActiveMdiChild) {
                    iTab = t.Index;
                    break;
                }
            }
            sftTabs1.Current = iTab;
        }
    }
}